package com.guguju.ssms.feature.Home;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;


import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.utils.ToastUtils;
import com.guguju.ssms.R;
import com.guguju.ssms.constant.IntentParms;
import com.guguju.ssms.core.base.BaseActivity;
import com.guguju.ssms.feature.me.MeFragment;

//import butterknife.BindView;

public class HomeActivity extends BaseActivity {


    ImageView iv_Homeindex;
    TextView tv_Homeindex;
    LinearLayout ll_HomeIndex;
    ImageView ivMe;
    TextView tvMe;
    LinearLayout llMe;



    ImageView bgGary;
    LinearLayout mProgressLogoView;

    //当前fragment
    private Fragment currentFragment;
    //首页fragment
    private IndexFragment indexFragment;
    //我的fragment
    private MeFragment meFragment;

    public static final int FRAGMENT_ME = 1;
    public static final int FRAGMENT_INDEX = 2;

    private Animation in, out;
    private Dialog dialog;
/*
    @BindView(R.id.bg_gary)
    ImageView bgGary;
    @BindView(R.id.progressView)
    LinearLayout mProgressLogoView;*/





    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_home);
        super.onCreate(savedInstanceState);

        iv_Homeindex=(ImageView)findViewById(R.id.iv_homeindex);
        tv_Homeindex=(TextView)findViewById(R.id.tv_homeindex);
        ll_HomeIndex=(LinearLayout)findViewById(R.id.ll_homeindex);

        ivMe=(ImageView)findViewById(R.id.iv_me);
        tvMe=(TextView)findViewById(R.id.tv_me);
        llMe =(LinearLayout)findViewById(R.id.ll_me);

        bgGary=(ImageView)findViewById(R.id.bg_gary);

       mProgressLogoView=(LinearLayout)findViewById(R.id.progressView);;


        int fragmentType = FRAGMENT_INDEX;
        if (getIntent() != null) {
            fragmentType = getIntent().getIntExtra(IntentParms.HOME_FRAGMENT_TYPE, FRAGMENT_ME);
        }
      to(fragmentType);
       //in = AnimationUtils.loadAnimation(this, R.anim.slide_bottom_in);
        //out = AnimationUtils.loadAnimation(this, R.anim.slide_bottom_out);

        llMe.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {

                to(FRAGMENT_ME);
            }

        });

        ll_HomeIndex.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {

                to(FRAGMENT_INDEX);
            }

        });
    }



    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

        int fragmentType = FRAGMENT_ME;
        if (intent != null) {
            fragmentType = intent.getIntExtra(IntentParms.HOME_FRAGMENT_TYPE, FRAGMENT_ME);
        }
        to(fragmentType);

        if (currentFragment instanceof MeFragment) {
            meFragment.onRefresh();
        }

        if (currentFragment instanceof IndexFragment) {
           indexFragment.onRefresh();
        }
    }

    @Override
    protected void onRestart() {
        super.onRestart();

        if (currentFragment instanceof MeFragment) {
            meFragment.onRefresh();
        }

    }

    @Override
    protected void onResume() {
        super.onResume();

    }


    private void to(int fragment) {
        switch (fragment) {
            case FRAGMENT_INDEX:
                tv_Homeindex.setTextColor(ContextCompat.getColor(this, R.color.gugujuMainYellow));

                tvMe.setTextColor(ContextCompat.getColor(this, R.color.textNormal666666));

                iv_Homeindex.setImageResource(R.drawable.ic_index_highlight);

                ivMe.setImageResource(R.drawable.ic_me_normal);


                showIndexFragment();
                break;

            case FRAGMENT_ME:

                tv_Homeindex.setTextColor(ContextCompat.getColor(this, R.color.textNormal666666));

                tvMe.setTextColor(ContextCompat.getColor(this, R.color.gugujuMainYellow));

                iv_Homeindex.setImageResource(R.drawable.ic_index_normal);

                ivMe.setImageResource(R.drawable.ic_me_highlight);

                showMeFragment();

                break;

        }
    }



    /**
     * 显示我的fragment
     */
    private void showMeFragment() {

        if (meFragment == null) {
            meFragment = MeFragment.newInstance();
        }
        addOrShowFragment(meFragment, MeFragment.TAG);
    }


    /**
     * 显示首页fragment
     */
    private void showIndexFragment() {
        if (indexFragment == null) {
            indexFragment = IndexFragment.newInstance();
        }
        addOrShowFragment(indexFragment, IndexFragment.TAG);

    }


    /**
     * 添加或者显示fragment
     *
     * @param fragment    切换的fragment
     * @param fragmentTag 切换的fragment的表示
     */
    private void addOrShowFragment(Fragment fragment, String fragmentTag) {
        if (currentFragment == fragment) {
            return;
        }

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        if (currentFragment != null) {
            transaction.hide(currentFragment);
        }

        if (!fragment.isAdded()) { // 如果当前fragment未被添加，则添加到Fragment管理器中
            transaction.add(R.id.frame, fragment, fragmentTag).commit();
        } else {
            transaction.show(fragment).commit();
        }
        currentFragment = fragment;
    }


    public void toMainFragment() {
        to(FRAGMENT_INDEX);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private long firstTime = 0;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (!(currentFragment instanceof IndexFragment)) {
                to(FRAGMENT_INDEX);
                return true;
            }
            long secondTime = System.currentTimeMillis();
            if (secondTime - firstTime > 2000) { //如果两次按键时间间隔大于2秒，则不退出
                ToastUtils.showShortToast(this, "再按一次退出");
                firstTime = secondTime;//更新firstTime
                return true;
            } else { //两次按键小于2秒时，退出应用
                System.exit(0);
            }
        }
        return super.onKeyDown(keyCode, event);
    }

}
